package myJs.user.workflow.featureSelection.uniAna

import cats.effect.SyncIO
import myJs.myPkg.jquery.{$, JQuery}
import myJs.tool.Tool
import myJs.user.workflow.featureSelection.AllFSSoft.FSSoftT
import org.scalajs.dom.{Element, document}
import scalatags.Text.all._
import shared.Pojo.UniAnaSoftT
import myJs.Implicits._

import scala.collection.immutable.SeqMap
import upickle.default._
import com.softwaremill.quicklens._
import japgolly.scalajs.react.vdom.all._
import myJs.myPkg.reactHookForm.{ReactHookForm, UseFieldArrayOptions, UseFieldArrayRT, UseFormRT}
import myJs.user.FeatureSelectionTool
import shared.SharedTool
import shared.pojo.WorkflowMission._
import myJs.projectCps._
import myJs.user.mission.boxplotResult

/** Created by yz on 2/12/2020
  */
object UniAna extends FSSoftT with UniAnaSoftT {

  override def refreshResult(idStr: String): Unit = {
    super.refreshResult(idStr)
    Tool.initMiniColors
    FeatureSelectionTool.resultShow(idStr)
    uniAnaTab.Cp.Props(idStr = idStr).render.renderIntoDOM(document.querySelector(".uniAna #root"))
  }

  def getIdStr(y: Element) = {
    getIdStrJq(y).attr("id").get
  }

  def getIdStrJq(y: Element) = {
    $(y).parents(".myArg")
  }

  override def cp(
      fieldArray: UseFieldArrayRT,
      fsData: FsData,
      fsDataChange: FsData => SyncIO[Unit],
      fsDataModifyF: (FsData => FsData) => SyncIO[Unit],
      showIdStr: String,
      extraData: ExtraData,
      extraDataChange: ExtraData => SyncIO[Unit]
  ) = {
    Cp.Props(
      fieldArray = fieldArray,
      fsData = fsData,
      fsDataChange = fsDataChange,
      fsDataModifyF = fsDataModifyF,
      showIdStr = showIdStr,
      extraData = extraData
    ).render
  }

  override def appendData(
      fieldArray: UseFieldArrayRT,
      fsData: FsData,
      fsDataChange: FsData => SyncIO[Unit],
      nodeId: String,
      extraData: ExtraData
  ) = {
    SyncIO {} >> {
      val uniAnaData = EachUniAnaData(nodeId = nodeId, uniAnaGroups = extraData.groups)
      val newFsData = fsData.modify(_.uniAna).using(_ :+ uniAnaData)
      fieldArray.append(uniAnaData)
      fsDataChange(newFsData)
    }
  }

  override def fieldArray(useForm: UseFormRT): UseFieldArrayRT = ReactHookForm.useFieldArray(
    UseFieldArrayOptions(
      control = useForm.control,
      name = "uniAna"
    )
  )

  override def removeData(fsData: FsData, fsDataChange: FsData => SyncIO[Unit], curI: Int) = {
    SyncIO {} >> {
      val newData = fsData.modify(_.uniAna).using(_.removed(curI))
      fsDataChange(newData)
    }
  }

}
